/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.directedit;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import com.cea.papyrus.core.commands.LabelDirectEditCommand;
import com.cea.papyrus.core.utils.Debug;

/**
 * This edit policy is used to compute direct edit requests.
 * It is intended to be used in conjunction with LabelDirectEditManager and LabelControler.
 */
public class LabelDirectEditPolicy extends DirectEditPolicy {

	/**
	 * Creates a new LabelDirectEditPolicy.
	 */
	public LabelDirectEditPolicy() {
	}

	/**
	 * Get the Command to edit the property.
	 * Called when the editor is closed and the change commited.
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	protected Command getDirectEditCommand(DirectEditRequest request) {
		Debug.debug(this, "LabelDirectEditPolicy.getDirectCommand()",10);

		String newValue = (String) request.getCellEditor().getValue();
		ILabelControler labelCtrl = (ILabelControler) request.getDirectEditFeature();
		LabelDirectEditCommand command = new LabelDirectEditCommand( labelCtrl.getPropertyAccessor(), newValue);
		return command;
	}

	/**
	 * Show the current edit value in the edited label.
	 * Called each time the value to edit is changed.
	 * 
	 * @param request 
	 */
	@Override
	protected void showCurrentEditValue(DirectEditRequest request) {
		// String value = (String) request.getCellEditor().getValue();
		//ILabelControler labelCtrl = (ILabelControler) request.getDirectEditFeature();
		// labelCtrl.getLabel().setText(value);
		// hack to prevent async layout from placing the cell editor twice.
		getHostFigure().getUpdateManager().performUpdate();
	}

}
